// import * as loginService from '@/api/login'
import { BasicLayout, BlankLayout, PageView, RouteView } from '@/layouts'

// 前端路由表
const constantRouterComponents = {
  // 基础页面 layout 必须引入
  BasicLayout: BasicLayout,
  BlankLayout: BlankLayout,
  RouteView: RouteView,
  PageView: PageView,
  403: () => import(/* webpackChunkName: "error" */ '@/views/exception/403'),
  404: () => import(/* webpackChunkName: "error" */ '@/views/exception/404'),
  500: () => import(/* webpackChunkName: "error" */ '@/views/exception/500'),
}

// 前端未找到页面路由（固定不用改）
const notFoundRouter = {
  path: '*',
  redirect: '/404',
  hidden: true,
}

// 根级菜单
const rootRouter = {
  Menu_KeyName: 'index',
  Menu_Url: '/',
  Menu_Address: 'BasicLayout',
  Menu_Redirect: '/dashboard',
  Menu_Title: '首页',
  children: [],
}

/**
 * 动态生成菜单
 * @param res
 * @returns {Promise<Router>}
 */
export const generatorDynamicRouter = (res) => {
  return new Promise((resolve, reject) => {
    const menuNav = []
    rootRouter.children = res
    menuNav.push(rootRouter)
    const routers = generator(menuNav)
    routers.push(notFoundRouter)
    resolve(routers)
  })
}

/**
 * 格式化树形结构数据 生成 vue-router 层级路由表
 *
 * @param routerMap
 * @param parent
 * @returns {*}
 */
export const generator = (routerMap, parent) => {
  return routerMap.map((item) => {
    const currentRouter = {
      // 如果路由设置了 path，则作为默认 path，否则 路由地址 动态拼接生成如 /dashboard/workplace
      path: item.Menu_Url || `${(parent && parent.path) || ''}/${item.Menu_KeyName}`,
      // 路由名称，建议唯一
      name: item.Menu_KeyName || '',
      // 该路由对应页面的 组件 :方案1
      // component: constantRouterComponents[item.component || item.key],
      // 该路由对应页面的 组件 :方案2 (动态加载)
      component: constantRouterComponents[item.Menu_Address] || (() => import(`@/views/${item.Menu_Address}`)),

      // meta: 页面标题, 菜单图标, 页面权限(供指令权限用，可去掉)
      meta: {
        title: item.Menu_Title,
        icon: item.Menu_Icon || undefined,
        // eslint-disable-next-line no-unneeded-ternary
        hiddenHeaderContent: item.Menu_IfPageHerder === '1' ? true : false,
        keepAlive: item.Menu_IfRouteCache,
        target: item.Menu_Open || '',
        permission: item.Menu_KeyName,
      },
    }
    // 是否设置了隐藏菜单
    if (item.Menu_IfLeftView) {
      // eslint-disable-next-line no-unneeded-ternary
      currentRouter.hidden = item.Menu_IfLeftView === '1' ? false : true
    }
    // 是否设置了隐藏子菜单
    if (item.Menu_IfHideChildren) {
      // eslint-disable-next-line no-unneeded-ternary
      currentRouter.hideChildrenInMenu = item.Menu_IfHideChildren === '1' ? true : false
    }
    // 为了防止出现后端返回结果不规范，处理有可能出现拼接出两个 反斜杠
    if (!currentRouter.path.startsWith('http')) {
      currentRouter.path = currentRouter.path.replace('//', '/')
    }
    // 重定向
    item.Menu_Redirect && (currentRouter.redirect = item.Menu_Redirect)
    // 是否有子菜单，并递归处理
    if (item.children && item.children.length > 0) {
      currentRouter.children = generator(item.children, currentRouter)
    }
    return currentRouter
  })
}
